# Create your models here.
from django.db import models
import datetime, re

# Create your models here.
class Entry(models.Model):
    title = models.CharField(maxlength=255)
    text = models.TextField()
    pub_date = models.DateTimeField('date published')

    published = models.BooleanField()
    char_re = re.compile("[\w]")

    def __str__(self):
        return self.title

    def url(self):
        return "/%d/%02d/%02d/%s/" % (self.pub_date.year, 
	    self.pub_date.month, self.pub_date.day, self.urlname())

    def urlname(self):
	newchars = []
	for char in self.title:
	    if self.char_re.match(char) is not None:
		newchars.append(char.lower())
	    elif char == " ":
		newchars.append("-")
	return "".join(newchars)

    @classmethod
    def get_published(cls):
        return cls.objects.filter(published=True)
    
    class Admin:
        pass

class Comment(models.Model):
    name = models.CharField(maxlength=255)
    url = models.CharField(maxlength=255)
    text = models.TextField()
    pub_date = models.DateTimeField("date published", auto_now_add=True)
    
    entry = models.ForeignKey(Entry)

    def has_url(self):
	return len(self.url.strip()) > 0

    def get_url(self):
	val = self.url.strip()
	if len(val) is 0:
	    return
	if val[:7] == "http://":
	    return val
	return "http://" + val

    def __str__(self):
	return self.text[:30]
    class Admin:
        pass
